package com.lanou.sinasports.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.sinasports.R;
import com.lanou.sinasports.adapter.EventsAdapter;
import com.lanou.sinasports.beans.Events;
import com.lanou.sinasports.beans.GsonEvents;
import com.lanou.sinasports.greenevents.Eve;
import com.lanou.sinasports.greenevents.EveDao;
import com.lanou.sinasports.utils.DaoSingleton;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class EventsFragment extends Fragment {
    private GridView gridView;
    private EventsAdapter adapter;
    private List<Events> data;
    private static VolleySingleton queue;
    private String url="http://interface.sina.cn/sports/sports_navs/client_sports_ctrl/client_sports_ctrl.d.json";
    private Context context;
    private EveDao eveDao;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d("EventsFragment", "onCreateView() called with: " + "inflater = [" + inflater + "], container = [" + container + "], savedInstanceState = [" + savedInstanceState + "]");
        return inflater.inflate(R.layout.fragment_events,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        gridView= (GridView) view.findViewById(R.id.gv_events);


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        data=new ArrayList<>();

        queue=VolleySingleton.getInstance();
        final List<Eve> databaseEvent=new ArrayList<>();
        eveDao= DaoSingleton.getInstance().getEveDao();

        GsonRequest<GsonEvents> request = new GsonRequest<>(url, GsonEvents.class, new Response.Listener<GsonEvents>() {
            @Override
            public void onResponse(GsonEvents gsonEvents) {
                Log.d("EventsFragment", gsonEvents.getResult().getData().getList().get(0).getTitle());
                int size = gsonEvents.getResult().getData().getList().size();
                for (int i = 0; i < size ;i++) {
                    Events events=new Events();
                    String name = gsonEvents.getResult().getData().getList().get(i).getTitle();
                    String newFeedId=gsonEvents.getResult().getData().getList().get(i).getNewFeedId();
                    String logoUrl=gsonEvents.getResult().getData().getList().get(i).getLogo();
                    events.setLogoId(logoUrl);
                    events.setName(name);
                    events.setNewFeedId(newFeedId);

                    data.add(events);
                    Eve eve=new Eve();
                    eve.setLogo(logoUrl);
                    eve.setName(name);

                    databaseEvent.add(eve);
                }

                eveDao.insertInTx(databaseEvent);
                adapter=new EventsAdapter(context,data);
                gridView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


                   adapter=new EventsAdapter(context,"11",getData());
                   gridView.setAdapter(adapter);


            }
        });

        queue._addRequest(request);




    }


    public List<Eve> getData(){
        if(eveDao!=null){
            return eveDao.queryBuilder().list();
        }else {
            return null;
        }


    }
}
